package com.example.sunmiv1.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.AllocationDetailAdapter;
import com.example.sunmiv1.adapter.OutAndInWarehouseDetailsAdapter;
import com.example.sunmiv1.entity.AllocationDetailInfo;
import com.example.sunmiv1.entity.WarehouseInAndOutDetailBean;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*門店調撥出庫詳情*/
public class AllocationDetailActivity extends Activity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.listView_depositDetail)
    ListView listViewDepositDetail;
    @BindView(R.id.rv)
    RecyclerView rv;
    private ACache aCache;
    private int type;//1.AllocationAdapter入庫詳情 2.AllocationAdapter出庫詳情 3.PurchaseOrderActivity門店採購單詳情 4.PurchaseOrderActivity商戶採購單詳情
    private Context context;
    private LoadingDialog loadingDialog;
    private AllocationDetailInfo allocationDetailInfo;
    private WarehouseInAndOutDetailBean warehouseInAndOutDetailBean;
    private List<AllocationDetailInfo.DataBean> list = new ArrayList<>();
    private List<WarehouseInAndOutDetailBean.DataBean> dataList = new ArrayList<>();
    private AllocationDetailAdapter allocationDetailAdapter;
    private OutAndInWarehouseDetailsAdapter outAndInWarehouseDetailsAdapter;  // 出入庫細節
    private static final String TAG = "AllocationDetailActivit";

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    setOutAndInWarehouseDetailsAdapter();
                    break;
                case 2:
                    setDepositDetailAdapter();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_deposit_detail);
        ButterKnife.bind(this);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        type = getIntent().getIntExtra("type", 1);
        Log.e(TAG, "onCreate: id=" + getIntent().getStringExtra("id"));
        if (type == 1 || type == 2) {
            if (type == 1) {
                titleTitle.setText(R.string.WarehousingDetails);
            } else {
                titleTitle.setText(R.string.DeliveryDetails);
            }
            getDepositDetail();
        } else if (type == 3 || type == 4) {
            titleTitle.setText(R.string.PurchaseOrderDetails);
            getPurchaseOrderDetail();
        }
    }


    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    private void setOutAndInWarehouseDetailsAdapter() {
        if (outAndInWarehouseDetailsAdapter == null) {
            outAndInWarehouseDetailsAdapter = new OutAndInWarehouseDetailsAdapter(this, dataList, type);
            rv.setLayoutManager(new LinearLayoutManager(this));
            rv.setAdapter(outAndInWarehouseDetailsAdapter);
        } else {
            outAndInWarehouseDetailsAdapter.notifyDataSetChanged();
        }
        outAndInWarehouseDetailsAdapter.setChoose(index -> {
            Intent intent = new Intent(context, StringCodeOperationActivity.class);
            if (type == 1) {
                intent.putExtra("operation", 18);
            } else {
                intent.putExtra("operation", 17);
            }
            intent.putExtra("id", dataList.get(index).getId());
            context.startActivity(intent);
        });
    }

    private void setDepositDetailAdapter() {
        if (allocationDetailAdapter == null) {
            allocationDetailAdapter = new AllocationDetailAdapter(context, list, type);
            listViewDepositDetail.setAdapter(allocationDetailAdapter);
        } else {
            allocationDetailAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 獲取出入庫詳情
     */
    private void getDepositDetail() {
        loadingDialog.show();
        String url = "";
        String detailType = "";
        if (type == 1) { // inWare
            if ("2".equals(aCache.getAsString("userType"))) {
                url = Common.STORE_ALLOCATION_DETAIL1;
                detailType = "3";
//                detailType="1";
            } else {
                url = Common.WAREHOUSING_LIST;
                detailType = "";
            }
        } else if (type == 2) {// outWare
            if ("2".equals(aCache.getAsString("userType"))) {
                url = Common.STORE_ALLOCATION_DETAIL;
                detailType = "";
            } else {
                url = Common.ALLOCATE_INFO;
                detailType = "";
            }

        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", getIntent().getStringExtra("id"));
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            if ("1".equals(aCache.getAsString("userType")) || "2".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("operator_id"));
            } else {
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }
            jsonObject.put("type", detailType);
            jsonObject.put("user_type", aCache.getAsString("userType"));
            HttpUtils.post(context, url, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    warehouseInAndOutDetailBean = GsonUtils.JsonToBean(text, WarehouseInAndOutDetailBean.class);
                    Message msg = new Message();
                    if (warehouseInAndOutDetailBean.getStatus() == 1) {
                        msg.what = 1;
                        dataList.addAll(warehouseInAndOutDetailBean.getData());
                    } else {
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /**
     * 獲取採購單詳情
     */
    private void getPurchaseOrderDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            String url = "";
            if (type == 3) {
                url = Common.STORE_PUR_DETAILS;
            } else if (type == 4) {
                url = Common.MER_PUR_DETAILS;
            }
            jsonObject.put("id", getIntent().getStringExtra("id"));
            HttpUtils.post(context, url, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    list.clear();
                    allocationDetailInfo = GsonUtils.JsonToBean(text, AllocationDetailInfo.class);
                    Message msg = new Message();
                    if (allocationDetailInfo.getStatus() == 1) {
                        list.addAll(allocationDetailInfo.getData());
                        msg.what = 2;
                    } else {

                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
